// 第一题
// 将下面对象转化成如下形式
// 输入
const entry = {
    a: {
        b:{
            c: {
                dd: 'abcdd'
            }
        },
        d: {
            ee: 'adee'
        },
        f: 'af'
    }
};

// 输出

const output = {
    'a.b.c.dd': 'abcdd',
    'a.d.ee': 'adee',
    'a.f': 'af'
};


// 思路分析
// 1、 递归思想
function flatObj(entry) {
    let result = {};
    function innerFn(entry, pre='') {
        for (const key in entry) {
            if (Object.hasOwnProperty.call(entry, key)) {
                const newKey = `${pre}${key}`;
                const item = entry[key];
                if (typeof item === 'object') {
                    innerFn(item, `${newKey}.`)
                } else {
                    result[newKey] = item;
                }
            }
        }
        return result;
    };
    return innerFn(entry);
};

let res1 = flatObj(entry);
console.log('flatObj=>', res1);



// 2 队列思想
function flatObj2(entry) {
    let queue = Object.entries(entry);
    // console.log('ok:', queue)
    let result = {};
    while(queue.length) {
        let [key, value] = queue.pop();
        for (const [k, v] of Object.entries(value)) {
            if (typeof v === 'object') {
                queue.push([`${key}.${k}`, v]);
            } else {
                result[`${key}.${k}`] = v;
            }
        }
    };
    return result;
}

let res2 = flatObj2(entry);
console.log('flatObj2=>', res1);


